package com.ibiaoke.system.service;

import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.LocationUtils;
import com.ibiaoke.system.dao.ShopDao;
import com.ibiaoke.system.entity.Shop;
import com.ibiaoke.system.entity.vo.XcxShopDetail;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class XcxShopServiceImpl implements XcxShopService{
    @Autowired
    ShopService shopService;

    @Autowired
    private ShopDao shopDao;

    @Override
    public List listSortedShops(Double longitude, Double latitude) {
        List<XcxShopDetail> XcxShopDetailList = new ArrayList<XcxShopDetail>();
            if (longitude == null || latitude == null) {
                throw new ServiceException("经纬度不能为空");
            }
            List<Shop> allShop = shopService.listAllShop();
            if (CollectionUtils.isNotEmpty(allShop)) {
                Collections.sort(allShop, (o1, o2) -> {
                    boolean o1Max = (o1 == null || o1.getDesLongitude() == null || o1.getDesLatitude() == null);
                    boolean o2Max = (o2 == null || o2.getDesLongitude() == null || o2.getDesLatitude() == null);
                    if (o1Max && o2Max) {
                        return 0;
                    }
                    if (o1Max) {
                        return 1;
                    }
                    if (o2Max) {
                        return -1;
                    }
                    Double distance1 = LocationUtils.getDistance(longitude, latitude, o1.getDesLongitude(), o1.getDesLatitude());
                    Double distance2 = LocationUtils.getDistance(longitude, latitude, o2.getDesLongitude(), o2.getDesLatitude());
                    return distance1.compareTo(distance2);
                });
            }

        if (allShop != null) {
            for(Shop shop :allShop){
                XcxShopDetail shopDetail =new XcxShopDetail();
                shopDetail.setId(shop.getShopId());
                shopDetail.setName(shop.getShopName());
                shopDetail.setBusinessTime(shop.getOpenBeginTime()+" - "+shop.getOpenEndTime().toString());
                Double distince = LocationUtils.getDistance(longitude, latitude, shop.getDesLongitude(), shop.getDesLatitude());
                DecimalFormat df = new DecimalFormat("######0.0");
                shopDetail.setDistince(Double.valueOf(df.format(distince/1000)));
                shopDetail.setShopowner(shop.getContactPerson());
                shopDetail.setMobile(shop.getShopMobile());
                shopDetail.setShopAddress(shop.getShopAddress());
                shopDetail.setDesLatitude(shop.getDesLatitude());
                shopDetail.setDesLongitude(shop.getDesLongitude());
                shopDetail.setStatus(shopDetail.getDistince() >= 3 ? 0 : 1);
                XcxShopDetailList.add(shopDetail);
            }
        }
        return XcxShopDetailList;
    }

    @Override
    public Shop getShpopById(Integer id) {
        return  shopDao.selectByPrimaryKey(id);
    }

    @Override
    public XcxShopDetail getNearXcxShop(List<XcxShopDetail> list) {
        for(XcxShopDetail xcxShopDetail:list){
            if(xcxShopDetail.getDistince() < 3){
                return  xcxShopDetail;
            }
        }
        return null;
    }
}
